package com.itheima.d2_xml;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;

/*
     目标：掌握使用Dom4j框架解析XML文件。
 */
public class Dom4JTest1 {
    public static void main(String[] args) throws DocumentException {
        //创建一个解析器对象
        SAXReader saxReader = new SAXReader();
        //读取xml
        Document read = saxReader.read(new File("E:\\heima\\daima\\DarHorse\\moudule03\\properties-xml-logback-app\\src\\USER.xml"));
        //获取根元素
        Element root = read.getRootElement();
        //获取根元素的子元素
        List<Element> elements = root.elements();
        for (Element element : elements) {
            Attribute idArrt = element.attribute("id");
            String id = idArrt.getValue();

            Element nameE = element.element("name");
            String name = nameE.getText();

            Element ageE = element.element("age");
            String age = ageE.getText();

            Element sexE = element.element("sex");
            String sex = sexE.getText();

            Element zwjsE = element.element("zwjs");
            String zwjs = zwjsE.getText();

            System.out.println("id="+id+",姓名="+name+",年龄="+age+",性别="+sex+",自我介绍="+zwjs);
        }
    }
}